from pickle import TRUE
import webview
import psutil
import json

from service.app import app_service


class Client:
    def __init__(self):
        self.window = None
        self.server = None

    def start(self):
        self.window = webview.create_window(
            '万能工具箱', 
            self.get_server_url(),
            width=1200,
            height=800,
            resizable=True,
            min_size=(800, 600),
            js_api= self
        )
        webview.start(debug=True)



    @staticmethod
    def get_server_url():
        return "http://localhost:5173"

    # Python方法，供JavaScript调用
    @staticmethod
    def get_system_info():
        """获取系统信息"""
        cpu_percent = psutil.cpu_percent()
        memory = psutil.virtual_memory()
        disk = psutil.disk_usage('/')
        
        return {
            'cpu_percent': cpu_percent,
            'memory_percent': memory.percent,
            'disk_percent': disk.percent
        }
    
    def show_message(self, message):
        """显示消息框"""
        return webview.windows[0].create_confirmation_dialog('消息', message)
    def get_app_store_list(self, request):
        return app_service.get_app_store_list(request)

    def get_installed_app_list(self, request):
        return app_service.get_installed_app_list(request)
    
    def configure_app(self, request):
        """配置应用"""
        return app_service.configure_app(request)

    def uninstall_app(self, request):
        """卸载应用"""
        return app_service.uninstall_app(request)

    def renew_app(self, request):
        """续费应用"""
        return app_service.renew_app(request)

    def start_app(self, request):
        """启动应用"""
        return app_service.start_app(request)

    def stop_app(self, request):
        """停止应用"""
        return app_service.stop_app(request)

    def call_js_function(self):
        """调用JavaScript方法的示例"""
        # 调用前端定义的updateSystemInfo方法
        self.window.evaluate_js('window.updateSystemInfo()')
        
        # 传递数据给前端
        system_info = self.get_system_info()
        self.window.evaluate_js(f'window.setSystemInfo({json.dumps(system_info)})')

client = Client()